import {defineStore} from 'pinia'

import zjRequest from "@/services";
import {IResponseData} from "@/services/types";
import {IActions, IState, VISUAL_URL_TYPE} from "@/store/visualizationModule/types";

const useVisualStore = defineStore<any, IState, any, IActions>('visualModule', {
    state: () => {
        return {
            prettyOccupyData: {
                occupyArr: [],
                percentageArr: [],
                titleNameArr: []
            },
            memberInductionData: {
                month: [],
                count: []
            },
            salaryRangeData: {
                label: [],
                realData: []
            },
            prettyCountData: {
                label: [],
                realData: []
            },
            memberInfoData: {
                avg_salary: 0,
                total_member: 0
            }
        }
    },
    actions: {
        // 获取统计靓号占有量
        async requestPrettyOccupyAction() {
            const res: IResponseData = await zjRequest.get({
                url: VISUAL_URL_TYPE.VISUAL_PRETTY_OCCUPY
            })
            if (res.code === 0) {
                let titleNameArr = []
                const allKey = res.data.all_key
                const occupyArr: Array<number> = []
                const percentageArr: Array<number> = []
                occupyArr.length = allKey.length
                percentageArr.length = allKey.length
                occupyArr.fill(0, 0, allKey.length)
                percentageArr.fill(0, 0, allKey.length)
                // TODO: 完善成一个for循环
                for (let item of res.data.occupy) {
                    occupyArr[item.level - 1] = item.total
                }
                for (let item of res.data.total) {
                    const occupyCount = occupyArr[item.level - 1]
                    console.log((occupyCount / item.total))
                    percentageArr[item.level - 1] = Math.ceil((occupyCount / item.total) * 100)
                }
                titleNameArr = res.data.all_key
                this.prettyOccupyData = {
                    occupyArr,
                    percentageArr,
                    titleNameArr
                }
            }
        },
        async requestMemberInductionAction(params) {
            const res: IResponseData = await zjRequest.get({
                url: VISUAL_URL_TYPE.VISUAL_MEMBER_INDUCTION,
                params
            })
            if (res.code === 0) {
                const month: Array<string> = []
                const count: Array<number> = []
                count.length = 12
                count.fill(0, 0)
                for (let i = 0; i < 12; i++) {
                    month[i] = i + 1 + "月"
                }
                for (let item of res.data) {
                    count[item.month - 1] = item.count
                }
                this.memberInductionData = {
                    month,
                    count
                }
            }
        },
        // 获取员工薪资范围
        async requestSalaryRangeCountAction() {
            const res = await zjRequest.get({
                url: VISUAL_URL_TYPE.VISUAL_SALARY_RANGE
            })
            if (res.code === 0) {
                this.salaryRangeData.label = res.data.map((item: any) => item.name)
                this.salaryRangeData.realData = res.data
                console.log(this.salaryRangeData)
            }
        },
        async requestPrettyCountAction() {
            const res: IResponseData = await zjRequest.get({
                url: VISUAL_URL_TYPE.VISUAL_PRETTY_COUNT
            })
            if (res.code === 0) {
                this.prettyCountData.label = res.data.map((item: any) => item.name + "级")
                this.prettyCountData.realData = res.data.map((item: any) => {
                    item.name = item.name + "级"
                    return item
                })
            }
        },
        async requestMemberInfoAction() {
            const res: IResponseData = await zjRequest.get({
                url: VISUAL_URL_TYPE.VISUAL_MEMBER_INFO
            })
            if (res.code === 0) {
                this.memberInfoData = res.data
            }
        }
    }
})
export default useVisualStore
